<!-- 公共文件 start -->
@extends('admin.public.base')
<!-- 公共文件 end -->

@section('content')
    <div class="layui-fluid">
        <div class="layui-row">
            <form class="layui-form">
                <div class="layui-card-header">
                    <button type="button" class="layui-btn layui-btn-danger"
                            onclick="discount_rate({{ $data['discount_rate'] }})"><i
                                class="layui-icon">&#xe735;</i>基础折扣率：{{ $data['discount_rate'] }}</button>
                    <button type="button" class="layui-btn"
                            onclick="xadmin.open('添加信息','/admin/store/storeGoodsAdd?id={{$data['id']}}')"><i
                                class="layui-icon"></i>添加新商品
                    </button>
                </div>
                <div class="layui-card-body layui-table-body layui-table-main">
                    <table class="layui-table layui-form">
                        <thead>
                        <tr>
                            <th>图片</th>
                            <th>商品名</th>
                            <th>商品分类</th>
                            <th>商品编号</th>
                            <th>标准价格</th>
                            <th>设定价格</th>
                            <th>最低起订量</th>
                            <th>销售策略(买)</th>
                            <th>销售策略(赠)</th>
                        </tr>
                        </thead>
                        <tbody>
                        @foreach( $goodsList as $kk => $item)
                            <tr>
                                <td><img src="{{ $item['goods_img'] }}" height="40px" alt="品牌图"></td>
                                <td>{{ $item['goods_name'] }}</td>
                                <td>{{ $item['cate_name'] }}</td>
                                <td>{{ $item['goods_sn'] }}</td>
                                <td>{{ $item['goods_price'] }}</td>
                                <td>
                                    @if ($item['fixed_price'] == 1 || $item['promotion'] == 1)
                                        <input type="number" readonly name="goods[{{$kk}}][sale_price]"
                                               value="{{$item['goods_price']}}" class="layui-input layui-disabled">
                                    @else
                                        <input type="number" name="goods[{{$kk}}][sale_price]" value="{{$item['sale_price']}}"
                                               class="layui-input sale_price_{{$kk}}" min='0'>
                                    @endif
                                    <input type="hidden" class="goods_price_{{$kk}}" value="{{$item['goods_price']}}">
                                    <input type="hidden" class="fixed_price_{{$kk}}" value="{{$item['fixed_price']}}">
                                </td>
                                <td>
                                    <input type="number" name="goods[{{$kk}}][mini_quantity]"
                                           value="{{$item['mini_quantity']}}" class="layui-input" min='1'>
                                </td>
                                <td>
                                    <input type="number" name="goods[{{$kk}}][buy_num]" value="{{$item['buy_num']}}"
                                           class="layui-input" min='1'>
                                </td>
                                <td>
                                    <input type="number" name="goods[{{$kk}}][get_num]" value="{{$item['get_num']}}"
                                           class="layui-input" min='0'>
                                    <input type="hidden" name="goods[{{$kk}}][goods_id]" value="{{$item['id']}}">
                                </td>
                            </tr>
                        @endforeach
                        </tbody>
                    </table>
                </div>
                <div class="layui-form-item">
                    <label for="L_repass" class="layui-form-label"></label>
                    <input type="hidden" value="{{ $data['id'] }}" name="id">
                    <button class="layui-btn" style="float: right;margin-right: 50px; margin-top:30px;width: 100px"
                            lay-filter="add" lay-submit="">提交</button>
                </div>
            </form>
        </div>
    </div>
    <script>
        var goodsNum = parseInt("{{$kk}}}");

        function discount_rate(discount_rate) {
            if (confirm("你确定要重置价格吗？")) {
                var fixed_price = 0;
                var goods_price = 0;
                var sale_price = 0;
                for (i = 0; i <= goodsNum; i++) {
                    fixed_price = $('.fixed_price_' + i).val();
                    if (fixed_price == 0) {
                        goods_price = $('.goods_price_' + i).val();
                        sale_price = $('.sale_price_' + i).val(roundFun(goods_price * discount_rate, 2));
                    }
                }
            }
        }
        layui.use(['form', 'layer', 'jquery'], function() {
            $ = layui.jquery;
            var form = layui.form,
                layer = layui.layer;

            //监听提交
            form.on('submit(add)', function(data) {
                console.log(data);
                //发异步，把数据提交给php
                $.ajax({
                    type: 'POST',
                    url: '/admin/store/storeGoodsEdit',
                    data: data.field,
                    dataType: 'json',
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
                    },
                    success: function(data) {
                        layer.alert(data.msg)
                        if (data.code == 1) {
                            //关闭当前frame
                            xadmin.close();
                            // 可以对父窗口进行刷新
                            xadmin.father_reload();
                        }
                    },
                    error: function(xhr, type) {
                        alert('Ajax error!')
                    }
                });
                return false;
            });

        });
        //保留n位小数
        function roundFun(value, n) {
            return Math.round(value * Math.pow(10, n)) / Math.pow(10, n);
        }
        //HTML反转义
        function HTMLDecode(text) {
            var temp = document.createElement("div");
            temp.innerHTML = text;
            var output = temp.innerText || temp.textContent;
            temp = null;
            return output;
        }
    </script>
@endsection